package com.zklh.test;


import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

//public class Demo1 {
//    public static void main(String[] args) throws IOException {
//        StringBuilder key = new StringBuilder();
//        key.append('1');
//        key.append('0');
//        key.append('1');
//        String m = key.toString();
//        int j = Integer.parseInt(m,2);
//        System.out.println(j);
//    }
//}
class Demo1 {
    int ret = Integer.MIN_VALUE;
    // int value = 0;
    public int longestUnivaluePath(TreeNode root) {
        if(root == null)return 0;
        help(root);
        return ret;
    }
    public int help(TreeNode root){
        if(root == null)return 0;
        int left = help(root.left);
        int right = help(root.right);

        int passLeft = 0;
        int passRight = 0;
        if (root.left != null && root.left.val == root.val) {
            passLeft+=1;
        }

        if (root.right != null && root.right.val == root.val) {
            passRight+=1;
        }

        ret = Math.max(ret, passLeft+passRight);
        return Math.max(passLeft,passRight);
    }

    public static void main(String[] args) {
        //1,0,1,0,1,0,1
        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(1);
        root.right = new TreeNode(3);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(1);
        root.right.left = new TreeNode(5);
        root.right.right = new TreeNode(6);

        Demo1 a = new Demo1();
        int m = a.longestUnivaluePath(root);
        System.out.println(m);
    }
}
  class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }


